package com.stormrage.edu.ctrl.edu;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.stormrage.edu.serv.edu.EduLecServ;
import com.stormrage.edu.serv.edu.EduProjectServ;
import com.stormrage.edu.serv.student.StuAcctServ;
import com.stormrage.entity.edu.EduLec;
import com.stormrage.entity.edu.EduProject;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: EduLecCtrl.java
 * @Package com.stormrage.edu.ctrl.edu
 * @Description: 讲师 Controller
 * @author www.stormragetech.com
 * @date 2016年4月16日
 * @version V1.0
 */
@Controller
@RequestMapping("/lec")
public class EduLecCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(EduLecCtrl.class);

	@Value("#{constants['html.title']}")
	private String htmlTitle;

	@Value("#{constants['html.logo']}")
	private String htmlLogo;

	@Value("#{constants['url.root']}")
	private String rootUrl;

	@Value("#{constants['path.image']}")
	private String imagePath;

	@Autowired
	private EduLecServ eduLecServ;

	@Autowired
	private EduProjectServ eduProjectServ;

	@Autowired
	private StuAcctServ stuAcctServ;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String list(Model model) {

		logger.debug("【 Controller 】-【 EduLecCtrl 】-【 List 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			if (currentUser.getPrincipal() != null)
				model.addAttribute("user", stuAcctServ.queryByAcct(currentUser.getPrincipal().toString()));

			// ------------------ 获取用户操作的 Menu ------------------
			List<EduProject> menus = eduProjectServ.queryList();

			// ------------------ 获取讲师List ------------------
			List<EduLec> array = eduLecServ.queryList();

			// ------------------ Model 赋值 ------------------
			model.addAttribute("ipath", rootUrl + imagePath);
			model.addAttribute("menus", menus);
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 EduLecCtrl 】-【 List 】-【 GET 】-【 COMPLETED 】");
		return "lec/list";

	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String lec(@PathVariable("id") Integer id, Model model) {

		logger.debug("【 Controller 】-【 EduLecCtrl 】-【 Lec 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			if (currentUser.getPrincipal() != null)
				model.addAttribute("user", stuAcctServ.queryByAcct(currentUser.getPrincipal().toString()));

			// ------------------ 获取用户操作的 Menu ------------------
			List<EduProject> menus = eduProjectServ.queryList();

			// ------------------ 获取讲师信息 ------------------
			EduLec entity = eduLecServ.queryById(id);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("ipath", rootUrl + imagePath);
			model.addAttribute("menus", menus);
			model.addAttribute("data", entity);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 EduLecCtrl 】-【 Lec 】-【 GET 】-【 COMPLETED 】");
		return "lec/lec";

	}

}
